# -*- coding: koi8-r -*-
## OJAB wtf&dfn module
## Copyright (C) Alexey Frolov <falexey@nm.ru>
## Copyright (C) Boris Kotov <admin@avoozl.ru>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.

import xmpp

messages=None
awtf=[]

def wtf(conn, msg, args, replyto, uone):
    ts=args.strip()
    to=[]
    for n in xrange(len(awtf)):
        if awtf[n][0]==ts:
            to.append(messages['wtf']%(awtf[n][0], awtf[n][1], awtf[n][2]))
    if to!=[]:
	if uone: to=to[-1:][0]
	else: to=',\n'.join(to)
	to="%s: "%ts+to+'.'
        conn.send(xmpp.protocol.Message(replyto, to, 'groupchat'))
    else:
        conn.send(xmpp.protocol.Message(replyto, messages['nowtf']%(msg.getFrom().getResource(), ts), 'groupchat'))

def wtfone(conn, msg, args, replyto):
    wtf(conn, msg, args, replyto, 1)

def wtfall(conn, msg, args, replyto):
    wtf(conn, msg, args, replyto, 0)

def dfn(conn, msg, args, replyto):
    text=args.strip()
    user=msg.getFrom().getResource()
    if text.find("=")+1:
        a=text.split("=")
        ns=-1
        for n in xrange(len(awtf)):
            if awtf[n][0:2]==[a[0].strip(),user]:
                ns=n
                break
        if ns>0: awtf[ns][2]=a[1].strip()
        else: awtf.append((a[0],user,a[1]))
        conn.send(xmpp.protocol.Message(replyto, messages['dfn']%(msg.getFrom().getResource(), a[0].strip()), 'groupchat'))        
    else:
        conn.send(xmpp.protocol.Message(replyto, messages['params']%msg.getFrom().getResource(), 'groupchat'))
